home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / dwyer / test.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-02  |  3.0 KB  |  144 lines

  1. /* Listing 6        Test program  */
  2.  
  3. /*
  4.     This test driver is meant to be linked with listings 2, 4, and 5,
  5.     and uses the Borland graphics library. Egavga.bgi needs to be 
  6.     in the same directory as the executable
  7. */
  8.  
  9.  
  10. #include <conio.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <graphics.h>
  14.  
  15.  
  16. #define SIN_COLOR 12
  17. #define COS_COLOR 12
  18.  
  19.  
  20. typedef short Trig;     /*  describes a 2.14 fixed point number */
  21. typedef short Angle;    /*  describes a 12.4 fixed point number */
  22.  
  23.  
  24. extern Trig cosine( Angle angle );
  25. extern Trig sine( Angle angle );
  26.  
  27. extern void FloatingPointLine( float x1, float y1, float x2, float y2 );
  28. extern void FixedPointLine( short x1, short y1, short x2, short y2 );
  29. extern void IterativeLine( short x1, short y1, short x2, short y2 );
  30.  
  31. static void DrawAxis( void );
  32. static void Label( const char *pLabel );
  33.  
  34.  
  35. void main( void )
  36. {
  37.     int driver = VGA, mode = VGAHI, errorcode;
  38.     Angle angle;
  39.     Trig sin, cos;
  40.     short screenX, screenY;
  41.     short centerX, centerY;
  42.  
  43.  
  44.     /* Init graphics */
  45.  
  46.     initgraph( &driver, &mode, "" );
  47.     if( ( errorcode = graphresult() ) != grOk )
  48.     {
  49.         printf( "Error: %s\n", grapherrormsg( errorcode ) );
  50.         exit( 10 );
  51.     }
  52.  
  53.     centerX = getmaxx() / 2;
  54.     centerY = getmaxy() / 2;
  55.  
  56.  
  57.     /* Draw Sin function */
  58.     clearviewport();
  59.     DrawAxis();
  60.     Label( "Sin" );
  61.  
  62.     for( angle=0x8000; angle<0x7fff; angle++ )
  63.     {
  64.         /* Get sine value, scale, and convert to integer */
  65.         sin = sine( angle );
  66.  
  67.         /* scale to screen */
  68.         screenX = angle >> 7;
  69.         screenY = sin >> 7;
  70.  
  71.         /* plot */
  72.         putpixel( centerX + screenX, centerY + screenY, SIN_COLOR );
  73.     }
  74.  
  75.     getch();
  76.  
  77.     /* Draw Cos function */
  78.     clearviewport();
  79.     DrawAxis();
  80.     Label( "Cos" );
  81.  
  82.     for( angle=0x8000; angle<0x7fff; angle++ )
  83.     {
  84.         /* Get sine value, scale, and convert to integer */
  85.         sin = cosine( angle );
  86.  
  87.         /* scale to screen */
  88.         screenX = angle >> 7;
  89.         screenY = sin >> 7;
  90.  
  91.         /* plot */
  92.         putpixel( centerX + screenX, centerY + screenY, SIN_COLOR );
  93.     }
  94.  
  95.     getch();
  96.  
  97.     /* Draw Lines */
  98.     clearviewport();
  99.     DrawAxis();
  100.     Label( "Lines" );
  101.  
  102.     FloatingPointLine( centerX, centerY - 50, centerX + 50, centerY );
  103.     FloatingPointLine( centerX, centerY + 50, centerX + 50, centerY );
  104.     FloatingPointLine( centerX - 50, centerY, centerX, centerY - 50 );
  105.     FloatingPointLine( centerX - 50, centerY, centerX, centerY + 50 );
  106.  
  107.     FixedPointLine( centerX, centerY - 100, centerX + 100, centerY );
  108.     FixedPointLine( centerX, centerY + 100, centerX + 100, centerY );
  109.     FixedPointLine( centerX - 100, centerY, centerX, centerY - 100 );
  110.     FixedPointLine( centerX - 100, centerY, centerX, centerY + 100 );
  111.  
  112.     IterativeLine( centerX, centerY - 150, centerX + 150, centerY );
  113.     IterativeLine( centerX, centerY + 150, centerX + 150, centerY );
  114.     IterativeLine( centerX - 150, centerY, centerX, centerY - 150 );
  115.     IterativeLine( centerX - 150, centerY, centerX, centerY + 150 );
  116.  
  117.     getch();
  118.  
  119.     /* DeInit */
  120.     closegraph();
  121. }
  122.  
  123.  
  124.  
  125. void DrawAxis( void )
  126. {
  127.     int maxx, maxy;
  128.  
  129.     /* get screen extents */
  130.     maxx = getmaxx();
  131.     maxy = getmaxy();
  132.  
  133.     /* Draw Axis */
  134.     line( maxx / 2, 0, maxx / 2, maxy );
  135.     line( 0, maxy / 2, maxx, maxy / 2 );
  136. }
  137.  
  138.  
  139. void Label( const char *pLabel )
  140. {
  141.     outtextxy( 5, 5, pLabel );
  142. }
  143.  
  144.